<template>
  <view>
    <!-- -->
    <permissionModel ref="permissionModel" :permissionContent="permissionContent1" />
    <model ref="model" :content="content" :openAppPermission="true" @toOpenAppPermission="toOpenAppPermission" />
  </view>
</template>
<script>
import permision from "@/js_sdk/wa-permission/permission.js"
import {
  openAppSettingPermissions,
  requestAndroidPermission
} from '@/common/index.js'
export default {
  name: 'DongxianUploadImg',
  props: {
    value: {
      type: [Array, String],
      default: ''
    },
    permissionContent: {
      type: String,
      default: '获取拨打电话的权限，是为了能够拨打电话'
    },
    permissionAlbumContent: {
      type: String,
      default: '获取拍照的权限，是为了能够上传头像'
    },
    singleNum: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    imageUrl: {
      get() {
        return this.value || []
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  data() {
    return {
      permissionContent1: '获取电话权限，是为了能够拨打电话',
      content: '',
      content1: '拨打电话需要打开电话权限，请到应用设置里面开启。',
      show: false,
      columns: [
        [{
          label: '相机',
          id: 1
        }, {
          label: '相册',
          id: 2
        }]
      ],
    };
  },

  mounted() {

  },

  methods: {
    open(phoneNumber) {
      // this.$refs.model.open()
      let {
        platform
      } = uni.getSystemInfoSync('systemInfo')
      if (platform == 'android') {
        this.androidPermission('callPhone', this.content1, this.permissionContent, phoneNumber)
      } else if (platform == 'ios') {
        this.callPhone(phoneNumber)
        // if (permision.judgeIosPermission('contact')) {
        // } else {
        //   permision.gotoAppPermissionSetting()
        // }
      }
    },
    androidPermission(type, content, permissionContent, phoneNumber) {
      console.log('%c [ type ]-101', 'font-size:13px; background:pink; color:#bf2c9f;', type)
      let options = {
        'callPhone': 'android.permission.CALL_PHONE'
      }
      let permission = options[type]
      // let permission = type === 'camera' ? 'CAMERA' : 'READ_EXTERNAL_STORAGE'
      console.log('%c [ permission ]-93', 'font-size:13px; background:pink; color:#bf2c9f;', permission)
      var Manifest = plus.android.importClass("android.Manifest");
      var MainActivity = plus.android.runtimeMainActivity();
      let result = MainActivity.checkSelfPermission(Manifest.permission[permission]);
      console.log('%c [ result ]-63', 'font-size:13px; background:pink; color:#bf2c9f;', result)
      if (result) {
        plus.android.requestPermissions([permission], e => {
          console.log('%c [ e ]-69', 'font-size:13px; background:pink; color:#bf2c9f;', e)
          if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
            this.content = content
            this.$refs.model.open()
            this.$refs.permissionModel.close()
          } else if (e.deniedPresent.length > 0) {
            this.$refs.permissionModel.close()
          } else if (e.granted.length > 0) {
            this.$refs.permissionModel.close(); // 别忘了关闭提示
            this.callPhone(phoneNumber)
          }
        })
        // this.uploadCamera(); // 有权限则直接调用方法
      } else {
        // this.uploadCamera()
        this.permissionContent1 = permissionContent
        this.$refs.permissionModel.open()
        // this.permissionContent = ''
        plus.android.requestPermissions([permission], e => {
          console.log('%c [ e ]-70', 'font-size:13px; background:pink; color:#bf2c9f;', e)
          if (e.deniedAlways.length > 0) { // 权限被拒绝或永久拒绝就给提示
            this.content = content
            this.$refs.permissionModel.close()
            this.$refs.model.open()
          } else if (e.deniedPresent.length > 0) {
            this.$refs.permissionModel.close()
          } else if (e.granted.length > 0) {
            this.$refs.permissionModel.close(); // 别忘了关闭提示
            this.callPhone(phoneNumber)
          }
        })
      }
    },
    callPhone(phoneNumber) {
      console.log('%c [ phoneNumber ]-123', 'font-size:13px; background:pink; color:#bf2c9f;', phoneNumber)
      uni.makePhoneCall({
        phoneNumber,
        success: (res) => {
          console.log('拨打电话成功')
          console.log(res)
        },
        fail: (err) => {
          console.log('拨打电话失败')
          console.log(err)
        }
      })
    },
    toOpenAppPermission() {
      console.log('toOpenAppPermission')
      this.$refs.model.close()
    }
  },
};
</script>